package SM.UI.Controls;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Table extends Widget{
	
	/*Variable Declaration*/
	
	private int Columns;
	private boolean Wrap = false;
	private int Cellspacing = 0;
	private int ColumnWidth = 0;
	
	/*Constructor*/
	
	public Table()
	{
		super();
	}
	
	public Table(String name, int width)
	{
		super(name, width);
	}
	
	public Table(String name, int width, String style)
	{
		super(name, width, style);
	}
	
	public Table(String name, int width, String style, int columns)
	{
		this(name, width, style);
		
		this.Columns = columns;
	}
	/*Getter-Setter*/
	
	/*public int GetWidth()
	{
		return this.Width;
	}
	
	public void SetWidth(int width)
	{
		this.Width = width;
	}
	
	public void SetName(String name)
	{
		this.Name = name;
	}
	
	public void SetStyle(String style)
	{
		this.Style = style;
	}
	
	public void SetDataSource(ResultSet dataSource)
	{
		this.DataSource = dataSource;
	}
	*/
	public void SetWrap(boolean wrap)
	{
		this.Wrap = wrap;
	}
	
	public void SetCellspacing(int spacing)
	{
		this.Cellspacing = spacing;
	}
	
	public void SetColumnWidth(int width)
	{
		this.ColumnWidth = width;
	}
	
	public void SetDataSource(ResultSet dataSource, int columns)
	{
		super.SetDataSource(dataSource);
		this.Columns = columns;
	}
	/*
	public void SetColumns(int columns)
	{
		this.Columns = columns;
	}
	
	public int GetColumns()
	{
		return this.Columns;
	}
	*/
	
	/*Functions*/
	
	public String getHTML()
	{
		return "";
	}
	
	public String getHTML(Connection connection)
	{
		try {
			
				if(connection == null || connection.isClosed())
				{
					System.out.println("Null Connection or Closed.. Cannot fetch data \n");
					return "";
				}
			
				String DisplayString = new String();
				
				String Class = (super.GetStyle() == "") ? "default" : super.GetStyle();
			
				//ResultSet resultSet = this.DataSource;
				
				ResultSet resultSet = super.GetDataSource();
				
				if(resultSet == null)
				{
					System.out.println("Null ResultSet.. Cannot fetch data");
					return "";
				}

				try
				{
				
					DisplayString = "<table name='" + super.GetName() +  "' width='" + super.GetWidth() + "%' class='" + Class + "' cellspacing = " + this.Cellspacing + ">\n";
				
					while(resultSet.next()) //while there are more rows
					{
						
						DisplayString += "<tr>\n";
						
							for(int Columns = 0; Columns < this.Columns; Columns++) //get columns
							{
								if(this.ColumnWidth != 0)
									DisplayString += "<td width='" +this.ColumnWidth + "%'>";
								else
									DisplayString += "<td>\n";
								
								if(this.Wrap)
									DisplayString += "<div style=\"word-break:break-all;\">\n";
									
									DisplayString += resultSet.getString(Columns+1) + "\n";
								
								if(this.Wrap)
									DisplayString += "</div>\n";
								
								DisplayString += "</td>\n";
							}
							
						DisplayString += "</tr>\n";
					}
				
					DisplayString += "</table>\n";
				
				}catch(SQLException SQLEx)
				{
					System.out.println("Oops!! Exception while generating Table data \n Code : " + SQLEx.getErrorCode() + "\n Message : " + SQLEx.getMessage());
				}
				
				return DisplayString;

		}catch(Exception SQLEx) {}
		
		finally{
			try {
			
					if(connection != null)
						connection.close();
					
			}catch(SQLException SQLEx)
			{
				System.out.println("Exception while closing streams");
			}
		}
			
		return "";
	}
	
	public String toString()
	{
		String DisplayString = new String();
		
		String Class = (super.GetStyle() == "") ? "default" : super.GetStyle();
	
		//ResultSet resultSet = this.DataSource;
		
		ResultSet resultSet = super.GetDataSource();
		
		if(resultSet == null)
		{
			System.out.println("Null ResultSet.. Cannot fetch data");
			return "";
		}
		
		try
		{
		
			DisplayString = "<table name='" + super.GetName() +  "' width='" + super.GetWidth() + "%' class='" + Class + "'>\n";
		
			while(resultSet.next()) //while there are more rows
			{
				DisplayString += "<tr>\n";
				
					for(int Columns = 0; Columns < this.Columns; Columns++) //get columns
					{
						DisplayString += "<td>\n";
						
							DisplayString += resultSet.getString(Columns);
						
						DisplayString += "</td>\n";
					}
					
				DisplayString += "</tr>\n";
			}
		
			DisplayString += "</table>\n";
		
		}catch(SQLException SQLEx)
		{
			System.out.println("Oops!! Exception while generating Table data \n Code : " + SQLEx.getErrorCode() + "\n Message : " + SQLEx.getMessage());
		}
		
		return DisplayString;
	}
}
